poll ={
	last_key : 0,
	last_q : 0,
	new_q : 0,
	last_f : 0,
	last_a : [],
	new_a : [],
	cateTypeDes : [
		'',
		'普通投票，单主题，分多选和单选两种',
		'投稿作品评选投票，同一个用户可以给多个作品投票',
		'PK投票，仅限2个选项，用户只能选其一',
		'普通的多主题投票,多项问题答完一起提交',
		'调查投票,用户在完成所需题目之后可增加表单提交用户的个人信息，活动用',
		'有奖问答,用户必须答对一题才能向下进行,可增加表单提交用户的个人信息，活动用'
	],
	tb_inner : '',
	addKeys : function(num){
		if(num < 1){
			return false;
		}
		var html = '';
		for(var i = 0;i<num;i++) {
			poll.last_key++;
			var tr = document.createElement('tr');
			var td1 = document.createElement('td');
			var td2 = document.createElement('td');
			var td3 = document.createElement('td');
			var td4 = document.createElement('td');
			td1.innerHTML = '选项：' + poll.last_key;
			td2.innerHTML = '<input type="text" id="key_' + poll.last_key + '" name="keys[]" value=""/>';
			td3.innerHTML = '初始票数:';
			td4.innerHTML = '<input type="text" id="num_' + poll.last_key + '" name="start_num[]" value="0"/>';
			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.appendChild(td4);
			tr.className='k_row';
			$('#keybody').append(tr);
		}
		$('#kbd').show();
	},
	
	addQuest : function(num){
		if(num < 1){
			return false;
		}
		var html = '';
		for(var i= 0;i<num;i++){
			poll.last_q++;
			var tr = document.createElement('tr');
			var td1 = document.createElement('th');
			var td2 = document.createElement('td');
			var td3 = document.createElement('td');
			var td4 = document.createElement('td');
			td2.style.textAlign='left';
			td4.style.textAlign='left';
			tr.id = 'row_que_' + poll.last_q;
			td1.innerHTML = '主题：' + poll.last_q;
			td2.innerHTML = '<input type="text" id="que_' + poll.last_q + '" name="question[' + poll.last_q + ']" value=""/>';
			td3.innerHTML = '选择类型:';
			td4.innerHTML = '<label for="radio_' + poll.last_q + '"><input type="radio" id="radio_' + poll.last_q + '" onclick="poll.showlimit(this);"  class="checkbox" name="type[' + poll.last_q + ']" value="radio" checked />单选</label>\
					<label for="checkbox_' + poll.last_q + '"><input type="radio" id="checkbox_' + poll.last_q + '" onclick="poll.showlimit(this);" class="checkbox" name="type[' + poll.last_q + ']" value="checkbox" />多选</label>\
					共<input type="text" class="sort_input" id="key_num' + poll.last_q + '" value="1" />项<button onclick="poll.addAnswer($(\'#key_num' + poll.last_q + '\').val(),' + poll.last_q + ');" type="button">添加</button>';
			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.appendChild(td4);
			tr.className='t_row';
			$('#quetions').append(tr);
			poll.last_a[poll.last_q] = 0;
		}
	},
	
	addNewQuest : function(num){
		if(num < 1){
			return false;
		}
		var html = '';
		for(var i= 0;i<num;i++){
			poll.new_q++;
			var tr = document.createElement('tr');
			var td1 = document.createElement('th');
			var td2 = document.createElement('td');
			var td3 = document.createElement('td');
			var td4 = document.createElement('td');
			td2.style.textAlign='left';
			td4.style.textAlign='left';
			tr.id = 'row_que_' + poll.new_q;
			td1.innerHTML = '主题：' + poll.new_q;
			td2.innerHTML = '<input type="text" id="que_' + poll.new_q + '" name="newquestion[' + poll.new_q + ']" value=""/>';
			td3.innerHTML = '选择类型:';
			td4.innerHTML = '<label for="radio_' + poll.new_q + '"><input type="radio" id="radio_' + poll.new_q + '" onclick="poll.showlimit(this);"  class="checkbox" name="newtype[' + poll.new_q + ']" value="radio" checked />单选</label>\
					<label for="checkbox_' + poll.new_q + '"><input type="radio" id="checkbox_' + poll.new_q + '" onclick="poll.showlimit(this);" class="checkbox" name="newtype[' + poll.new_q + ']" value="checkbox" />多选</label>\
					共<input type="text" class="sort_input" id="key_num' + poll.new_q + '" value="1" />项<button onclick="poll.addNewAnswer($(\'#key_num' + poll.new_q + '\').val(),' + poll.new_q + ');" type="button">添加</button>';
			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.appendChild(td4);
			tr.className='t_row';
			$('#quetions').append(tr);
			poll.new_a[poll.new_q] = 0;
		}
	},
	
	addNewAnswer : function(num,qid){
		if(num < 1){
			return false;
		}
		var html = '';
		var flag =  false;
		for(var k in poll.new_a){
			if(flag == true){
				var next_q = k;
				break;
			}
			if(k == qid){
				flag =  true;
			}
		}
		var ass = poll.new_a;
		var voteType = parseInt(document.forms[0].vcateid.value);
		for(var i= 0;i<num;i++){
			poll.new_a[qid]++;
			var tr = document.createElement('tr');
			var td1 = document.createElement('td');
			var td2 = document.createElement('td');
			var td3 = document.createElement('td');
			var td4 = document.createElement('td');
			td2.style.textAlign='left';
			td4.style.textAlign='left';
			td1.innerHTML = '选项：' +  poll.new_a[qid];
			td2.innerHTML = '<input type="text" id="key_' + qid + '_' + poll.new_a[qid] + '" name="newkeys[' + qid + '][]" value=""/>' +(voteType == 6 ? (' <label for="isa_' + qid + '_' + poll.new_a[qid] + '"><input type="checkbox" class="checkbox" id="isa_' + qid + '_' + poll.new_a[qid] + '" name="newisanswer[' + qid + '][]" value="1" />正确答案</label>') : '');
			td3.innerHTML = '初始票数:';
			td4.innerHTML = '<input type="text" id="num_' + qid + '_' + poll.new_a[qid] + '" name="newstart_num[' + qid + '][]" value="0"/>';
			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.appendChild(td4);
//			tr.className='k_row';
			if($('#row_que_'+next_q).length > 0){
				$(tr).insertBefore('#row_que_'+next_q);
			}else{
				$('#quetions').append(tr);
			}
		}
	},
	
	addAnswer : function(num,qid){
		if(num < 1){
			return false;
		}
		var t = $('quetions');
		var html = '';
		var next_q = qid + 1;
		var voteType = parseInt(document.forms[0].vcateid.value);
		for(var i= 0;i<num;i++){
			poll.last_a[qid]++;
			var tr = document.createElement('tr');
			var td1 = document.createElement('td');
			var td2 = document.createElement('td');
			var td3 = document.createElement('td');
			var td4 = document.createElement('td');
			td2.style.textAlign='left';
			td4.style.textAlign='left';
			td1.innerHTML = '选项：' +  poll.last_a[qid];
			td2.innerHTML = '<input type="text" id="key_' + qid + '_' + poll.last_a[qid] + '" name="keys[' + qid + '][]" value=""/>' +(voteType == 6 ? (' <label for="isa_' + qid + '_' + poll.last_a[qid] + '"><input type="checkbox" class="checkbox" id="isa_' + qid + '_' + poll.last_a[qid] + '" name="isanswer[' + qid + '][]" value="1" />正确答案</label>') : '');
			td3.innerHTML = '初始票数:';
			td4.innerHTML = '<input type="text" id="num_' + qid + '_' + poll.last_a[qid] + '" name="start_num[' + qid + '][]" value="0"/>';
			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.appendChild(td4);
//			tr.className='k_row';
			if($('#row_que_'+next_q).length > 0){
				$(tr).insertBefore('#row_que_'+next_q);
			}else{
				$('#quetions').append(tr);
			}
		}
	},
	
	addfields : function (num){
		if(num < 1){
			return false;
		}
		var html = '';
		for(var i= 0;i<num;i++){
			poll.last_f++;
			var tr = document.createElement('tr');
			var td1 = document.createElement('td');
			var td2 = document.createElement('td');
			var td3 = document.createElement('td');
			td2.style.textAlign='left';
			td3.style.textAlign='left';
			td1.innerHTML = '资料：' + poll.last_f;
			td2.innerHTML = '<select id="type_' + i + '" name="fieldtype[]">\
				<option value="text">text</option>\
				<option value="file">file</option>\
			</select>';
			td3.innerHTML = '<input type="text" id="field_' + i + '" name="fields[]" value=""/>';
			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			tr.className='k_row';
			$('#fields').append(tr);
		}
	},
	
	
	showlist : function (obj){
		var html = '';
		var voteType = parseInt(obj.value);
		var voteTpLi = obj.parentNode.parentNode;
		poll.last_key = poll.last_q = poll.last_f = 0;
		poll.last_a = [];
		$('#kbd').html('<table id="keybody" width="98%" class="raceTable" cellspacing="0" cellpadding="5"><tbody></tbody></table>');
		if($('#userFiled').length > 0)
			$('#userFiled').remove();
		if($('#li_field').length > 0)
			$('#li_field').remove();
		if($('#li_roll').length > 0)
			$('#li_roll').remove();
		switch(voteType){
			case 1:
				html = '<label for="radio"><input type="radio" id="radio"  class="checkbox" name="type" value="radio" checked />单选</label>\
					<label for="checkbox"><input type="radio" id="checkbox"  class="checkbox" name="type" value="checkbox" />多选</label>\
					共<input type="text"  class="sort_input" id="key_num" value="1" />项<button onclick="poll.addKeys($(\'#key_num\').val());" type="button">添加</button>';
				break;
			case 2:
				html = '<div>共<input type="text"  class="sort_input" id="key_num" value="1" />项投稿<button onclick="poll.addKeys($(\'#key_num\').val());" type="button">添加</button></div>';
				break;
			case 3:
				html = '<input type="hidden" style="width:20px;" name="type" value="radio" />';
				$('#kbd').html('<table id="keybody"  class="raceTable">\
					<tr class="k_row">\
						<td>选项：1</td>\
						<td><input type="text" id="key_1" name="keys[]" value=""/></td>\
						<td>初始票数:</td>\
						<td><input type="text" id="num_1" name="start_num[]" value="0"/></td>\
					</tr>\
					<tr class="k_row">\
						<td>选项：2</td>\
						<td><input type="text" id="key_2" name="keys[]" value=""/></td>\
						<td>初始票数:</td>\
						<td><input type="text" id="num_2" name="start_num[]" value="0"/></td>\
					</tr>\
				</table>');
				break;
			case 4:
				html = '<div>共添加<input type="text"  class="sort_input" id="que_num" value="1" />项主题<button onclick="poll.addQuest($(\'#que_num\').val());" type="button">添加</button></div>';
				$('#kbd').html('<table id="quetions" class="raceTable" width="98%"></table>');
				break;
			case 5:
			case 6:
				html = '<div>共添加<input type="text"  class="sort_input" id="que_num" value="1" />项主题<button onclick="poll.addQuest($(\'#que_num\').val());" type="button">添加</button></div>';
				$('#kbd').html('<table id="quetions" class="raceTable" width="98%"></table>');
				var userfiled = '<li id="userFiled">\
					<strong>用户资料填写项：</strong>\
					<span id="fieldConents">\
						共<input type="text"  class="sort_input" id="field_num" value="1" />项<button onclick="poll.addfields($(\'#field_num\').val());" type="button">添加</button>\
					</span>\
				</li>\
				<li id="li_field"><table id="fields" width="98%" class="raceTable"></table></li>';
				//alert(userfiled);
				var roll_row = '<li id="li_roll"><strong>开启前台抽奖</strong>\
					<span><label for="isroll"><input type="checkbox" class="checkbox" id="isroll" name="isroll" value="1" />是</label>\
				</li>';
				$(roll_row).insertAfter($(voteTpLi));
				$(userfiled).insertBefore('#sbmt');
				break;
		}
		if($('#showlimit')){
			$('#showlimit').remove();
		}
		$('#ctate_des').html(poll.cateTypeDes[voteType]);
		$('#addKeyConents').html(html);
	},
	
	checkCreate : function (obj){
		if(obj.vtitle.value == ''){
			alert('no title');
			return false;
		}
		if(obj.department_name.value == 0){
			alert('no department');
			return false;
		}
		var tp = obj.vcateid.value;
		switch(tp){
			case '1':
			case '2':
			case '3':
				var keys = document.getElementsByName('keys[]');
				if(keys.length < 2){
					alert('not enough keys');
					return false;
				}
				var f = 0;
				for(var i=0;i<keys.length;i++){
					if($.trim(keys[i].value) != '')
						f++;
				}
				if(f < 2){
					alert('have empty key');
					return false;
				}
				break;
			case '4':
				if(this.checkList() == false){
					return false;
				}
				alert(1);
				break;
			case '5':
				if(this.checkList() == false){
					return false;
				}
				alert(2);
				break;
		}
		//return false;
	},
	checkList : function(){
		var inputs = document.getElementById('quetions').getElementsByTagName('input');
		var len = inputs.length;
		for(var i=0;i<len;i++){
			var row = inputs[i];
			var row_len = row.id.length;
			if(row.id.substr(0,3) == 'que' && row.value !=''){
				var qid = row.id.substr(4,row_len);
//				alert(qid);
				var keyName = 'keys['+qid+'][]';
				var keys = document.getElementsByName(keyName);
				if(keys.length < 2){
					alert('not enough keys' +qid);
					alert(keys.length);
					return false;
				}
				var f=0;
				for(var j=0;j<keys.length;j++){
					if($.trim(keys[j].value) != '')
						f++;
				}
				if(f < 2){
					alert('have empty keys in '+qid);
					return false;
				}
				if($('#checkbox_'+qid)[0].checked == true && parseInt($('#maxlimit_'+qid).val()) > 0 && parseInt($('#minlimit_'+qid).val()) > parseInt($('#maxlimit_'+qid).val())){
					alert('min > max')
					return false;
				}
			}
		}
//		return true;
	},
	showlimit : function(obj){
		var id,idn,iid;
		var form = document.forms[0];
		var tp =form.vcateid.value;
		if(tp <4){
			id=idn='';
		}else{
			var attr = obj.id.split('_');
			if(attr.length > 1){
				id=attr[1];
				idn='['+ id +']';
				iid='_' + id;
			}
		}
		html = '<span id="showlimit'+id+'">\
			选择下限<input type="text" class="sort_input" id="minlimit'+iid+'" name="minlimit'+idn+'" value="1" />\
			上限<input type="text" class="sort_input" id="maxlimit'+iid+'" name="maxlimit'+idn+'" value="0" />上限设为0为不限制\
		</span>';
		var chepre = obj.id.substr(0,8);
		if($('#showlimit'+id).length < 1 && chepre == 'checkbox'){
			if (tp <4){
				$(html).insertAfter($(obj.parentNode.parentNode));
			}else{
				$(obj.parentNode.parentNode).append($(html));
			}
		}else{
			if($('#showlimit'+id).length > 0 && chepre != 'checkbox'){
				$('#showlimit'+id).remove();
			}
		}
	}
};
